package org.wkg.lesson10;

import java.util.concurrent.locks.ReentrantLock;

public class Demo_1107 {
    public static void main(String[] args) throws InterruptedException {
        Counter1107 counter = new Counter1107();


        // 创建两个线程对一个变量进时累加
        Thread t1 = new Thread(() -> {
            // 5万次
            for (int i = 0; i < 50000; i++) {
                counter.increase();
            }
        });

        /*// 线程2
        Thread t2 = new Thread(() -> {
            // 5万次
            for (int i = 0; i < 50000; i++) {
                counter.increase();
            }
        });*/
        long start = System.currentTimeMillis();
        // 启动线程
        t1.start();
        //t2.start();

        // 等待线程执行完成
        t1.join();
        //t2.join();
        long end = System.currentTimeMillis();
        // 查看运行结果
        System.out.println("count = " + counter.count);
        System.out.println("time = " + (end - start));
    }
}


class Counter1107 {
    public int count = 0;
    ReentrantLock lock = new ReentrantLock();

    public synchronized void increase() {
            count++;
    }


}